package graph;

import java.util.*;
import java.io.*;

import zclTool.*;

public class DrawGraphMatrix2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// load 0204uniq
		HashSet<Integer> train01ids = new HashSet<Integer>();
		HashSet<Integer> doublecheck = new HashSet<Integer>();
		DelimitedReader dr = new DelimitedReader("trainids");
		String line[];
		while ((line = dr.read()) != null) {
			int a = Integer.parseInt(line[0]);
			train01ids.add(a);
		}
		dr.close();
		HashMap<Integer, Integer> name2id = new HashMap<Integer, Integer>();
		HashMap<Integer,String>attributeName = new HashMap<Integer,String>();
		ArrayList<ArrayList<Integer>> clusters = new ArrayList<ArrayList<Integer>>();
		
		dr = new DelimitedReader("attributes");
		while((line = dr.read())!=null){
			int a = Integer.parseInt(line[0].trim());
			String b = line[2];
			attributeName.put(a, b);
		}
		dr.close();
		dr = new DelimitedReader("0204uniqes_id_sort");
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
				"drawMatrix_20"));
		OutputStreamWriter osw_b = new OutputStreamWriter(new FileOutputStream(
				"border_20"));
		OutputStreamWriter osw_c = new OutputStreamWriter(new FileOutputStream(
		"new_golden_standard"));
		//line = dr.read();// give up the first 572 largest cluster
		int ln = 0;
		while ((line = dr.read()) != null) {
			ln++;
			// System.out.println("Cluster i\t"+ln+"\t"+line[0]);
			ArrayList<Integer> onecluster = new ArrayList<Integer>();
			String[] attids = line[1].split(",");
			for (int i = 0; i < attids.length; i++) {
				int a = Integer.parseInt(attids[i]);
				doublecheck.add(a);
				if (train01ids.contains(a)) {
					onecluster.add(a);
					// int b = name2id.size();
					// name2id.put(a, b);
					// System.out.print(b+" ("+a+") ");
				}
			}
			clusters.add(onecluster);
		}
		dr.close();
		//double check
		Iterator<Integer> it1 = train01ids.iterator();
		while(it1.hasNext()){
			int a = it1.next();
			if(!doublecheck.contains(a)){
				System.out.println(a);
			}
		}
		Collections.sort(clusters, new Comparator<ArrayList<Integer>>() {
			public int compare(ArrayList<Integer> c1, ArrayList<Integer> c2) {
				return c2.size() - c1.size();
			}
		});
		for(int i = 0;i<clusters.size();i++){
			if(clusters.get(i).size()==0)continue;
			int a = clusters.get(i).get(0);
			osw_c.write(attributeName.get(a)+"\t"+clusters.get(i).size()+"\n");
		}
		osw_c.close();
//		for (int i = 0; i < clusters.size(); i++) {
//			ArrayList<Integer> c = clusters.get(i);
//			System.out.println("cluster " + i);
//			for (int k = 0;;k++) {
//				int a = c.get(k);
//				int b = name2id.size();
//				String name = attributeName.get(a);
//				name2id.put(a, b);
//				System.out.print(b + " (" + name + ") ");
//			}
//			System.out.println();
//			osw_b.write(name2id.size()+"\n");
//
//		}

//		dr = new DelimitedReader("train");
//		while ((line = dr.read()) != null) {
//			int id1 = Integer.parseInt(line[0]);
//			int id2 = Integer.parseInt(line[1]);
//			double val = Double.parseDouble(line[2]);
//			if (name2id.containsKey(id1) && name2id.containsKey(id2)) {
//				int nid1 = name2id.get(id1);
//				int nid2 = name2id.get(id2);
//				osw.write(nid1 + "\t" + nid2 + "\t" + val + "\n");
//			}
//		}
		osw.close();
		osw_b.close();

	}

}
